package com.star.service.Impl;

import com.star.dao.UserDao;
import com.star.entity.User;
import com.star.service.UserService;
import com.star.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User checkUser(String username, String password) {
        User user = userDao.findByUsernameAndPassword(username, MD5Utils.code(password));
        return user;
    }

    @Override
    public Integer addUser(User user, MultipartFile file, HttpServletRequest request) {
        //处理文件
        if (file != null) {
            String contextPath = request.getServletContext().getContextPath();
            long uploadTime = System.currentTimeMillis();
            String objectUrl = contextPath + "/" + uploadTime + "_" + file.getOriginalFilename();
            File filePath = new File(objectUrl);
            if (!filePath.exists()) {
                try {
                    filePath.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!StringUtils.isEmpty(objectUrl)) {
                try {
                    InputStream inputStream = file.getInputStream();
                    FileOutputStream outputStream = new FileOutputStream(objectUrl);
                    byte[] b = new byte[1024];
                    int inputLength;
                    while ((inputLength = inputStream.read(b)) > 0) {
                        outputStream.write(b, 0, inputLength);
                    }
                } catch (IOException e) {
                }
            }
            user.setAvatar(objectUrl);
        }else {
            user.setAvatar("https://img2.woyaogexing.com/2020/06/19/ab12df6fa29247e690ea8416bbf77199!400x400.jpg");
        }
        user.setPassword(MD5Utils.code(user.getPassword()));
        user.setType(2);
        user.setCreateTime(new Date());
        return userDao.addUser(user);
    }

    @Override
    public Integer update(User user, MultipartFile file, HttpServletRequest request) {
        //处理文件
        if (file != null) {
            String contextPath = request.getServletContext().getContextPath();
            long uploadTime = System.currentTimeMillis();
            String objectUrl = contextPath + "/" + uploadTime + "_" + file.getOriginalFilename();
            File filePath = new File(objectUrl);
            if (!filePath.exists()) {
                try {
                    filePath.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!StringUtils.isEmpty(objectUrl)) {
                try {
                    InputStream inputStream = file.getInputStream();
                    FileOutputStream outputStream = new FileOutputStream(objectUrl);
                    byte[] b = new byte[1024];
                    int inputLength;
                    while ((inputLength = inputStream.read(b)) > 0) {
                        outputStream.write(b, 0, inputLength);
                    }
                } catch (IOException e) {
                }
            }
            user.setAvatar(objectUrl);
        }
        user.setUpdateTime(new Date());
        return userDao.updateUser(user);
    }

    @Override
    public Integer deleteById(Integer id) {
        return userDao.deleteById(id);
    }

    @Override
    public User getById(Integer id) {
        return userDao.getById(id);
    }

    @Override
    public List<User> getList() {
        return userDao.getList();
    }
}